import 'package:chewie/chewie.dart';
import 'package:flutter/material.dart';
import 'package:flutter_barrage/flutter_barrage.dart';
import 'package:video_player/video_player.dart';
/// FileName 连文科
///
/// @Author/// @Date 2022/7/12 15:47
///
/// @Description 视频
class VideoPlayWidget extends StatefulWidget {
  final String path;
  const VideoPlayWidget({Key key,this.path}) : super(key: key);

  @override
  _VideoPlayWidgetState createState() => _VideoPlayWidgetState();
}

class _VideoPlayWidgetState extends State<VideoPlayWidget> {
  VideoPlayerController _videoPlayerController;
  ChewieController _controller;
  BarrageWallController _wallController;
  TextEditingController _editingController;
  bool f = true;
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _videoPlayerController = VideoPlayerController.network(widget.path);
    _controller = ChewieController(videoPlayerController: _videoPlayerController,
    autoInitialize: true,
    autoPlay: false,
    aspectRatio: 16/9);
    _wallController = BarrageWallController();
    _editingController = TextEditingController();
  }
  //避免内存泄漏
  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    _videoPlayerController.dispose();
    _controller.dispose();
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Stack(
        children: [
          AspectRatio(aspectRatio: _controller.aspectRatio,
          child: BarrageWall(
              controller: _wallController,
              child: Chewie(controller: _controller)),),
          Positioned(
            right: 35,
            bottom: 12,
            child: Switch(value: f, onChanged: (value){
              setState(() {
                f = value;
              });
              f ? _wallController.enable() : _wallController.disable();
            }),
          ),
          Positioned(
            right: 100,
            bottom: 25,
            child: Container(
              height: 30,
              width: 200,
              decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(8),
                color: Colors.white30
              ),
              child: Row(
                children: [
                  Expanded(child: TextField(
                    controller: _editingController,
                    decoration: InputDecoration(
                      border: InputBorder.none
                    ),
                  )),
                  TextButton(onPressed: (){
                    if(_editingController.text.isNotEmpty){
                      _wallController.send([
                        Bullet(child: Text(_editingController.text,style: TextStyle(fontSize: 16,color: Colors.greenAccent),))
                      ]);
                    }
                  }, child: Text('发送'))
                ],
              ),
            ),
          )
        ],
      ),
    );
  }
}
